package cn.com.gridinfo.sooc.base;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.base.ISetupToolBar;
import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.widget.SystemBarTintManager;
import com.jeremy.arad.widget.dialog.ProgressHUD;
import com.umeng.analytics.MobclickAgent;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.utils.MessageUtils;


/**
 * 应用 base activity
 * @author jieli
 */
public class BaseSoocActivity extends AppCompatActivity implements ISetupToolBar,SoocNetResult {

    ProgressHUD mProgressHUD; //进度加载
    private TextView mTitle;//头部标题名称dfsfdsdf
    private ImageView mLeftButton;//左边图标按钮
    private ImageView mRightButton;//右边图标按钮
    private View toolbarLine;//二级页面竖线
    ImageView toolbarRightimg;//右边页面图标
    TextView toolbarRighttv;//右边文字
    private Toolbar mActionBarToolbar;



    public SystemBarTintManager mTintManager;


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        getActionBarToolbar();

        mTintManager = new SystemBarTintManager(this);
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setTintColor(getResources().getColor(R.color.colorPrimaryDark));
    }


    protected Toolbar getActionBarToolbar() {
        if (mActionBarToolbar == null) {
            mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
            mTitle = (TextView) findViewById(R .id.toolbar_title);
            mLeftButton = (ImageView) findViewById(R.id.toolbar_leftbtn);
            mRightButton = (ImageView) findViewById(R.id.toolbar_rightbtn);
            toolbarLine = (View)findViewById(R.id.toolbar_line);
            toolbarRightimg = (ImageView)findViewById(R.id.toolbar_rightimg);
            toolbarRighttv = (TextView) findViewById(R .id.toolbar_righttv);
            if (mActionBarToolbar != null) {
                setSupportActionBar(mActionBarToolbar);
            }
        }
        return mActionBarToolbar;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mTitle != null && setupToolBarTitle() != null)
            mTitle.setText(setupToolBarTitle());

        if (mLeftButton != null) {
            if (setupToolBarLeftButton(mLeftButton)) {
                mLeftButton.setVisibility(View.VISIBLE);
            } else {
                mLeftButton.setVisibility(View.GONE);
            }
        }

        if (mRightButton != null) {
            if (setupToolBarRightButton(mRightButton)) {
                mRightButton.setVisibility(View.VISIBLE);
            } else {
                mRightButton.setVisibility(View.GONE);
            }
        }

        if ( toolbarLine!= null) {
            if (setToolbarLine(toolbarLine)) {
                toolbarLine.setVisibility(View.VISIBLE);
            } else {
                toolbarLine.setVisibility(View.GONE);
            }
        }

        if ( toolbarRightimg!= null) {
            if (setToolbarRightimg(toolbarRightimg)) {
                toolbarRightimg.setVisibility(View.VISIBLE);
            } else {
                toolbarRightimg.setVisibility(View.GONE);
            }
        }

        if ( toolbarRighttv!= null) {
            if (setToolbarRighttv(toolbarRighttv)) {
                toolbarRighttv.setVisibility(View.VISIBLE);
            } else {
                toolbarRighttv.setVisibility(View.GONE);
            }
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        AnimUtil.intentSlidOut(this);
    }

    @Override
    public void finish() {
        super.finish();
        AnimUtil.intentSlidOut(this);
    }

    public void noAnimfinish() {
        super.finish();
    }
    // 动态改变
    @Override
    public String setupToolBarTitle() {
        return null;
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @Override
    public boolean setupToolBarRightButton(ImageView rightButton) {
        return false;
    }

    @Override
    public TextView getmTitle() {
        return mTitle;
    }

    @Override
    public ImageView getmLeftButton() {
        return mLeftButton;
    }

    @Override
    public ImageView getmRightButton() {
        return mRightButton;
    }

    public View getToolbarLine() {
        return toolbarLine;
    }

    public boolean setToolbarLine(View toolbarLine) {
        return true;
    }

    public ImageView getToolbarRightimg() {
        return toolbarRightimg;
    }

    public boolean setToolbarRightimg(ImageView toolbarRightimg) {
        return false;
    }

    public TextView getToolbarRighttv() {
        return toolbarRighttv;
    }

    public boolean setToolbarRighttv(TextView toolbarRighttv) {
        return false;
    }

    /**
     * 请求成功
     * @param requestCode 网络请求顺序号，第一个请求，NetRequestOrderNum=0,处理第一条请求的结果。如果等于1,
     */
    @Override
    public void onRequestSuccess(int requestCode) {

    }

    /**
     * 请求失败
     * @param requestCode 请求链接 code
     * @param errorNo  请求网络失败 code 如404等
     * @param errorMessage  请求网络错误信息
     */
    @Override
    public void onRequestFaild(int requestCode,String errorNo, String errorMessage) {
        //两种进度加载方式，点击空白区域进度条是否取消
        showProgress(false);
        showProgress(false,false);
        MessageUtils.showImageShortToast(this, errorMessage);
    }

    /**
     * 未登录处理
     */
    @Override
    public void onInvalidLogin() {
//        AlertDialog.Builder bl = new AlertDialog.Builder(this)
//                .setMessage("请登陆")
//                .setPositiveButton(R.string.app_name,new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
////                        ScoreApplication.getContext().restartApp();
//                    }
//                })
//                .setCancelable(false);
//        bl.create().show();
//
//        Log.i("BaseActivity", "重启");
//        MessageUtils.showShortToast(this,"请登录");
//        IntentUtil.intentPushUp(this, LoginActivity.class,00);
    }

    /**
     * 无网络处理
     */
    @Override
    public void onNoConnect() {
        showProgress(false);
        showProgress(false, false);
        MessageUtils.showImageShortToast(this, "无网络连接");
    }

    /**
     * 进度加载 点击可取消加载进度条
     * @param show
     */
    public void showProgress(boolean show) {
        showProgress(show,false);
    }

    /**
     * 进度加载 点击可取消加载进度条
     * @param show
     * @param isCancle 点击是否可取消进度加载
     */
    public void showProgress(boolean show,boolean isCancle) {
        showProgressWithText(show, "加载中...",isCancle);
    }

    public void showProgressWithText(boolean show, String message,boolean isCancle) {
        if (show) {
            mProgressHUD = ProgressHUD.show(this, message, true, isCancle, null);
        } else {
            if (mProgressHUD != null) {
                mProgressHUD.dismiss();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Arad.http.cancelRequests(this);
    }
}
